package com.bdgls.fileformat;

import com.bdgls.common.TextFileHelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yibo on 2016-5-11.
 */
public class CsvConverter {

    public static List<List<String>> parse(String srcPath) throws Exception {

        List<List<String>> all = new ArrayList<List<String>>();

        String msg = TextFileHelper.getTextByFile(srcPath);
        List<String> line = new ArrayList<>();
        StringBuilder part = new StringBuilder();
        boolean hasQuota = false;
        int index = 0;
        while (index < msg.length()) {
            char c = msg.charAt(index);
            if (c == '"') {
                if (!hasQuota) {
                    //开始
                    hasQuota = true;
                    index++;
                    continue;
                } else {
                    //处理中
                    int nextIndex = index + 1;
                    if (nextIndex >= msg.length()) {
                        break;
                    }

                    char c1 = msg.charAt(nextIndex);
                    if (c1 == ',') {
                        index++;
                        hasQuota = false;
                        continue;
                    }

                    if (c1 == '\r') {
                        index++;
                        hasQuota = false;
                        continue;
                    }

                    if (c1 == '\n') {
                        index++;
                        hasQuota = false;
                        continue;
                    }

                    if (c1 == '"') {
                        index += 2;
                        part.append(c);
                        continue;
                    }

                    throw new Exception("bad format");
                }
            }

            if (c == ',') {
                if (hasQuota) {
                    part.append(c);
                    index++;
                    continue;
                } else {
                    String parMsg = part.toString();
                    part.setLength(0);
                    line.add(parMsg);
                    index++;
                    continue;
                }
            }

            //回车
            if (c == '\r') {
                index++;
                continue;
            }

            //换行
            if (c == '\n') {
                if (hasQuota) {
                    part.append(c);
                    index++;
                    continue;
                } else {
                    String parMsg = part.toString();
                    part.setLength(0);
                    line.add(parMsg);
                    all.add(line);
                    line = new ArrayList<>();
                    index++;
                    continue;
                }
            }

            //普通字符
            part.append(c);
            index++;
        }

        //尾巴
        if (part.length() > 0) {
            line.add(part.toString());
            part.setLength(0);
            all.add(line);
        }


        return all;

    }

    public static List<List<String>> parse2(String srcPath) throws Exception {

        List<List<String>> all = new ArrayList<>();
        String msg = TextFileHelper.getTextByFile(srcPath);
        List<String> line = new ArrayList<>();
        StringBuilder part = new StringBuilder();
        boolean prevIsQuota = false;
        int prevCharIndex = -1;
        int quotaCount = 0;
        int startQuotaIndex = -1;
        int index = -1;
        char[] buff = new char[4];
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(srcPath))));

        int read = br.read(buff, 0, buff.length);
        while (read != -1) {

            for (int i = 0; i < read; i++) {
                index++;
                char c = buff[i];

                if (c == '"') {

                    if (quotaCount == 0) {
                        //段开头
                        quotaCount++;
                        startQuotaIndex = index;
                        prevIsQuota = true;
                    } else {
                        //
                        if (prevIsQuota && prevCharIndex > startQuotaIndex) {
                            //吃掉相邻的引号
                            quotaCount--;
                            prevIsQuota = false;
                        } else {

                            part.append(c);
                            quotaCount++;
                            prevIsQuota = true;
                        }
                    }

                } else if (c == '\r') {
                    //donothing
                    prevIsQuota = false;

                } else if (c == ',') {

                    if (quotaCount == 0 || quotaCount == 2) {
                        //结束part
                        if (part.length() > 0 && part.substring(part.length() - 1).equals("\"")) {
                            //移去最后一个"
                            part.setLength(part.length() - 1);
                        }

                        String parMsg = part.toString();
                        part.setLength(0);
                        line.add(parMsg);

                        quotaCount = 0;
                        startQuotaIndex = -1;

                    } else {
                        //未结束，当作普通字符
                        part.append(c);
                    }

                    prevIsQuota = false;
                } else if (c == '\n') {
                    if (quotaCount == 0 || quotaCount == 2) {
                        //结束行
                        String parMsg = part.toString();
                        part.setLength(0);
                        line.add(parMsg);
                        all.add(line);
                        line = new ArrayList<>();

                        quotaCount = 0;
                        startQuotaIndex = -1;
                    } else {
                        //未结束,当作普通字符
                        part.append(c);
                    }

                    prevIsQuota = false;
                } else {

                    //普通字符
                    part.append(c);
                    prevIsQuota = false;
                }

                //
                prevCharIndex = index;

            }

            read = br.read(buff, 0, buff.length);
        }

        //尾巴
        if (part.length() > 0) {
            line.add(part.toString());
            part.setLength(0);
            all.add(line);
        }

        return all;

    }
}
